package com.budwk.app.sqfwV2.service.weiqingmen;

import com.budwk.app.sqfw.dto.SqfwCompleteFlowUpdateDTO;
import com.budwk.app.sqfw.enums.SqfwFormSummaryFieldEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.services.SqfwFormSummaryPdfService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService.CompleteFlowCommonPdfService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.result.GeneratorPdfWordResult;
import com.budwk.app.sqfwDoc.dto.CompleteFlowTemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.enums.weiqingmen.SafetyCheckEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.ThreadPoolExecutorUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-04-11  09:06
 * @Description: TODO
 */
public class BasePdfService extends CompleteFlowCommonPdfService {
    public static final String forSummaryNewConsName = "formSummaryNew";
    public static final String completeFlowConsName = "completeFlow";

    /**
     * 创建pdf
     * @param safetyCheckEnum
     * @param paramForController
     */
    public void createPdf(SafetyCheckEnum safetyCheckEnum, CompleteFlowPdfParamForController paramForController) {
        ThreadPoolExecutorUtil.getInstance().execute(() -> {

            SqfwFormSummaryPdfService formSummaryPdfService = NutzIocUtils.getBean(SqfwFormSummaryPdfService.class);

            PdfGenerateParam pdfGenerateParam = getPdfGenerateParam(paramForController);
            pdfGenerateParam.setPdfCreateConfigList(paramForController.getPdfCreateConfigList());

            // 创建pdf
            GeneratorPdfWordResult generateResult = super.generatePdf(pdfGenerateParam);
            String url = generateResult.getPdfAndWordResult().getPdfUrl();
            if (StringUtils.isBlank(url)) return;

            Sqfw_formSummary_new formSummaryNew = paramForController.getCompleteFlowDataDTO().getFormSummary();
            int index = Integer.parseInt(safetyCheckEnum.getCode());

            Cnd cnd = Cnd.NEW();
            cnd.and("summary_id", "=", formSummaryNew.getId())
                    .and("index", "=", index);
            Sqfw_form_summary_pdf pdf = formSummaryPdfService.fetch(cnd);

            if (pdf == null) {
                pdf = new Sqfw_form_summary_pdf();
                pdf.setIndex(index);
                pdf.setUrl(url);
                pdf.setStatus(StringUtils.isBlank(url) ? "1" : "2");
                pdf.setSummaryId(formSummaryNew.getId());
                pdf.setCreateDate(formSummaryNew.getCreateDate());
                pdf.setUpdateDate(new Date());
                try {
                    formSummaryPdfService.insert(pdf);
                } catch (Exception e) {
                }
            } else {
                if (StringUtils.isNotBlank(url)) {
                    pdf.setUrl(url);
                }
                pdf.setStatus(StringUtils.isBlank(url) ? "1" : "2");
                pdf.setUpdateDate(new Date());
                formSummaryPdfService.update(pdf);
            }
        });
    }

    /**
     * 构建数据
     * @param paramForController
     * @return
     */
    public PdfGenerateParam getPdfGenerateParam(CompleteFlowPdfParamForController paramForController) {
        SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
        String userId = paramForController.getUserId();
        Sys_user sysUser = sysUserService.fetch(userId);

        CompleteFlowTemplateCodeUpdateDTO formSummaryWithFlow = paramForController.getCompleteFlowDataDTO();

        SqfwCompleteFlowUpdateDTO completeFlow = formSummaryWithFlow.getCompleteFlow();
        completeFlow.setUserId(sysUser.getId());
        completeFlow.setUserName(sysUser.getUsername());
        completeFlow.setResourcePath("creating");
        completeFlow.setCreateDate(new Date());


        Sqfw_formSummary_new formSummary = formSummaryWithFlow.getFormSummary();
        formSummary.setParamData(formSummary.getParamData());
        formSummary.setCreateDate(new Date());
        formSummary.setCreateBy(userId);
        formSummary.setSqfwId(completeFlow.getSqfwId());
        formSummary.setWorkOrderId(completeFlow.getWorkerId());
        formSummary.setSqfwFlowId(completeFlow.getId());
        formSummary.setCreateUserName(sysUser.getUsername());
        formSummary.setCreateUserPhone(sysUser.getLoginname());
        formSummary.setUpdateBy(sysUser.getId());
        formSummary.setUpdateDate(new Date());
        formSummary.setUpdateUserName(sysUser.getUsername());
        formSummary.setUpdateUserPhone(sysUser.getLoginname());
        formSummary.setType(getSummaryType());
        formSummary.setProvince(sysUser.getProvince());
        formSummary.setCity(sysUser.getCity());
        formSummary.setCounty(sysUser.getCounty());
        formSummary.setDistrict(sysUser.getDistrict());
        formSummary.setCommunity(sysUser.getCommunity());
        formSummary.setCheckStatus(SqfwFormSummaryFieldEnum.check_status_init.getCode());
        formSummary.setUnitName(sysUser.getUnitName());
        formSummary.setUnitId(sysUser.getUnitId());

        if (formSummary.getFillDate() == null) {
            formSummary.setFillDate(new Date());
        }

        Map<String, Object> businessDataMap = new HashMap<>();
        businessDataMap.putAll(Map.of(forSummaryNewConsName, formSummary));
        businessDataMap.putAll(Map.of(completeFlowConsName, completeFlow));

        return getPdfGenerateParamFromBusinessData(completeFlow, formSummary, businessDataMap);
    }

    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String jasonStr) {
        return null;
    }

    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam) {

    }

    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {
        return null;
    }

    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        return null;
    }

    @Override
    protected String getSummaryType() {
        return null;
    }
}
